package algorithm;

import java.util.Random;

/**
 * @author Timi
 * 
 */
public class Rule4Cube extends Rule1Cube {
	
	private int d; // rule parameter
	byte[] s; // constant strategy

	public Rule4Cube(int n, int S, int R, int T,int P, boolean periodic, int d) {
		super(n, S, R, T, P, periodic);
		this.d = d;
		s = new byte[nvert];
	}
	
	public byte[] init() {
		
		Random generator = new Random();
		byte L[] = super.init();
		
		for (int i = 0; i < nvert; i++) s[i] = -1;
		
		for (int i = 0; i < d; i++) {
			int rand = generator.nextInt(nvert);
			while (s[rand] != -1) rand = generator.nextInt(nvert);
			s[rand] = L[rand];
		}
		
		return L;
	}
	/** 
	 * @see algorithm.Algorithm#cycle(byte[])
	 */
	public void cycle(byte[] L) {
		super.cycle(L);
		for (int i = 0; i < nvert; i++) 
			if (s[i] >= 0 && L[i] != s[i]) {
				L[i] = s[i];
				if (L[i] == 0) C--;
				else C++;
			}
	}

}
